package com.itheima.test2;

public class Test {
    public static void main(String[] args) {
        useAnimal(new Dog());
        useAnimal(new Cat());
    }
    public static void useAnimal(Animal a){
        a.eat();
        //a.wacthHome();

//        Dog dog = (Dog) a;
//        dog.watchHome();  // ClassCastException  类型转换异常

        // 判断a变量记录的类型, 是否是Dog
        if (a instanceof Dog){
            ((Dog) a).watchHome();
        }
    }
}

abstract class Animal{
    public abstract void eat();
}

class Dog extends Animal{
    public void eat(){
        System.out.println("狗吃肉");
    }
    public void watchHome(){
        System.out.println("狗看家");
    }
}
class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

}